package com.kai.steering.data.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.service.impl.BaseServiceImpl;
import com.kai.steering.common.web.vo.ReturnCodeExt;
import com.kai.steering.data.service.entity.domainObject.NoteDO;
import com.kai.steering.data.service.entity.domainObject.QuestionDO;
import com.kai.steering.data.service.entity.domainObject.UserQuestionDO;
import com.kai.steering.data.service.entity.po.Note;
import com.kai.steering.data.service.entity.po.Question;
import com.kai.steering.data.service.entity.po.UserQuestion;
import com.kai.steering.data.service.mapper.QuestionMapper;
import com.kai.steering.data.service.mapper.UserQuestionMapper;
import com.kai.steering.data.service.service.NoteService;
import com.kai.steering.data.service.service.QuestionService;
import com.kai.steering.data.service.service.UserQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserQuestionServiceImpl extends BaseServiceImpl<UserQuestionMapper, UserQuestion> implements UserQuestionService {
    @Autowired
    private UserQuestionMapper userQuestionMapper;
    @Autowired
    private NoteService noteService;
    @Autowired
    private QuestionService questionService;
    @Override
    public UserQuestionDO getUserQuestionById(String userQuestionId) {
        UserQuestion userQuestion = userQuestionMapper.getUserQuestionById(userQuestionId);
        UserQuestionDO userQuestionDO = new UserQuestionDO(userQuestion);
        return userQuestionDO;
    }

    @Override
    public UserQuestionDO getUserQuestion(String userId, String questionId, boolean noteExist) {
        QueryWrapper<UserQuestion> qw = new QueryWrapper<>();
        qw.eq("question_id", questionId);
        qw.eq("user_id", userId);
        UserQuestion userQuestion = this.getOne(qw);
        UserQuestionDO userQuestionDO = new UserQuestionDO(userQuestion);
        if (noteExist && userQuestion != null && userQuestion.getNoteId() != null) {
            //获取笔记
            NoteDO noteById = noteService.getNoteById(userQuestion.getNoteId());
            userQuestionDO.setNoteDO(noteById);
        }
        return userQuestionDO;
    }
    @Override
    public List<UserQuestionDO> getUserQuestions(String userId, List<String> questionIdS,boolean noteExist){
        QueryWrapper<UserQuestion> qw = new QueryWrapper<>();
        qw.in("question_id", questionIdS);
        qw.eq("user_id", userId);
        List<UserQuestion> userQuestions = this.baseMapper.selectList(qw);
        List<UserQuestionDO> userQuestionDOS = UserQuestionDO.convertUserQuestionDOS(userQuestions);

        for (int i = 0; i < userQuestionDOS.size(); i++) {
            if (noteExist && userQuestionDOS.get(i) != null ) {
                //获取笔记
                NoteDO noteById = noteService.getNoteById(userQuestions.get(i).getNoteId());
                userQuestionDOS.get(i).setNoteDO(noteById);
            }

        }


        return userQuestionDOS;
    }

    @Override
    public  Map<String ,UserQuestionDO> getUserQuestionMap(String userId, List<String> questionIdS,boolean noteExist){
        List<UserQuestionDO> userQuestions = getUserQuestions(userId, questionIdS, noteExist);
        HashMap<String, UserQuestionDO> stringUserQuestionDOHashMap = new HashMap<>();
        for (UserQuestionDO userQuestion : userQuestions) {
            stringUserQuestionDOHashMap.put(userQuestion.getQuestionId(),userQuestion);
        }
        return stringUserQuestionDOHashMap;
    }

    public UserQuestionDO saveUserQuestion(UserQuestionDO userQuestionDO){
        UserQuestion userQuestion = new UserQuestion(userQuestionDO);
        boolean save = save(userQuestion);
        if (save){
            return new UserQuestionDO(userQuestion);
        }else {
            return null;
        }
    }




    @Override
    public boolean updateQuestionData(UserQuestionDO userQuestionDO) {
        UserQuestionDO userQuestionById = getUserQuestionById(userQuestionDO.getUserQuestionId());
        if (userQuestionById == null || userQuestionById.getUserQuestionId() == null) {
            throw ReturnCodeExt.BUSINESS_EXCEPTION.getException(userQuestionDO.getUserQuestionId() + "不存在");
        }
        UserQuestion userQuestion = new UserQuestion(userQuestionDO);
        return  updateById(userQuestion);

    }

    @Override
    public UserQuestionDO updateQuestionData(String questionId, int correct) {
        /**
         * 参数检查
         */
        QuestionDO question = questionService.getQuestionById(questionId, null);
        if (question == null || question.getQuestionId() == null) {
            throw ReturnCodeExt.BUSINESS_EXCEPTION.getException(questionId + "不已存在");
        }
        /**
         * 获取数据+构建对象
         */
        UserQuestionDO userQuestionDO = getUserQuestion("000000", questionId, false);


        /**
         * 存储数据
         */
        if (userQuestionDO == null || userQuestionDO.getUserQuestionId() == null) {
            userQuestionDO.setQuestionId(questionId);
            userQuestionDO.setUserId("000000");
            userQuestionDO.setProblemNumber(1);
            userQuestionDO.setErrorNumber(correct == 0 ? 1 : 0);
            UserQuestionDO userQuestionDoSave = saveUserQuestion(userQuestionDO);
           return userQuestionDoSave;
            }
        userQuestionDO.setProblemNumber(userQuestionDO.getProblemNumber() + 1);
        userQuestionDO.setErrorNumber(userQuestionDO.getErrorNumber() + (correct == 0 ? 1 : 0));
        boolean b = updateQuestionData(userQuestionDO);
        if (!b){
            return null;
        }

        return  userQuestionDO;
    }

}
